package xyz.renyumeng.xkm.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static xyz.renyumeng.xkm.rabbitMQ.RabbitMqConstant.*;


/**
 * @author renyumeng
 * @date Created in 21:46 2023/7/6
 */

@Configuration
public class TtlProductQueueConfig {

    // 延时交换机
    @Bean
    public DirectExchange delayProductExchange() {
        return new DirectExchange(DELAY_PRODUCT_EXCHANGE);
    }

    // 死信交换机
    @Bean
    public DirectExchange deadLetterProductExchange() {
        return new DirectExchange(DEAD_LETTER_PRODUCT_EXCHANGE);
    }

    // 延时队列
    @Bean
    public Queue delayProductQueue() {
        Map<String, Object> args = new HashMap<>();
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_PRODUCT_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_PRODUCT_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL 1000 * 60 * 2
        args.put("x-message-ttl", 1000 * 60 * 2);
        return QueueBuilder.durable(DELAY_PRODUCT_QUEUE).withArguments(args).build();
    }

    // 死信队列
    @Bean
    public Queue deadLetterProductQueue() {
        return new Queue(DEAD_LETTER_PRODUCT_QUEUE);
    }

    // 延时队列绑定关系
    @Bean
    public Binding delayBindProduct() {
        return BindingBuilder
                .bind(delayProductQueue())
                .to(delayProductExchange())
                .with(DELAY_PRODUCT_ROUTING_KEY);
    }

    // 死信队列绑定关系
    @Bean
    public Binding deadLetterBindProduct() {
        return BindingBuilder
                .bind(deadLetterProductQueue())
                .to(deadLetterProductExchange())
                .with(DEAD_LETTER_PRODUCT_ROUTING_KEY);
    }


}
